package com.zsrt.javaweb;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class UserNameFilter implements Filter {
	
	private FilterConfig fc;
	
	
	public void init(FilterConfig config) throws ServletException {
		this.fc=config;
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		String username = request.getParameter("username");
		String webUsername = fc.getInitParameter("username");
		if(!username.equals(webUsername)){
			//放置错误提示消息
			request.setAttribute("message", "用户名错误...");
			
			//如果username不等于"scott"就将请求转发到login.jsp
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			
			//表示不执行后面的方法 不交给下一个过滤器处理
			return;
		}
		chain.doFilter(request, response);
	}

	

}
